//
//  MyClassViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/7/13.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "MyClassViewController.h"
#import "ClassStuDentViewController.h"
#import "MyClassTableViewCell.h"
#import "MyClassModel.h"
#import "KeBiaoListViewController.h"
#import "LessonTeacherViewController.h"
@interface MyClassViewController ()<UITableViewDelegate,UITableViewDataSource,ClassTabDidSelectDelegate>
{
    NSInteger page;
}

@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;



@end

@implementation MyClassViewController

-(NSMutableArray *)listArr
{
    if (!_listArr)
    {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV) {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.backgroundColor = [UIColor whiteColor];
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        [_listV registerNib:[UINib nibWithNibName:@"MyClassTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyClassTableViewCell"];
        _listV.separatorStyle = 0;
    }
    return _listV;
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=TeacherCourse&a=courseList" parmDic:@{@"tid":[[NSUserDefaults standardUserDefaults]objectForKey:@"tid"],@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {

        NSArray *Arr = [MyClassModel ModelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }
        [self.listArr addObjectsFromArray:Arr];
        [self.listV reloadData];
        [self.listV.mj_header endRefreshing];
        [self.listV.mj_footer endRefreshing];
        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }



//        self.listArr = [MyClassModel ModelConfigure:data];
//
//        [self.listV reloadData];
    } error:^(NSError *error) {

    }];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"教学班级";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    page = 1;
    [self.view addSubview:self.listV];

    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self Request];
    }];
    self.listV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self doRefreshSenior];
    }];

    [self.listV.mj_header beginRefreshing];

    

//    [self Request];
}

-(void)doRefreshSenior
{
    page = 1;
    [self Request];
}


#pragma mark ---代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.listArr.count == 0) {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

/*@property (weak, nonatomic) IBOutlet UIImageView *titleImageV;
 @property (weak, nonatomic) IBOutlet UILabel *ClassName;
 @property (weak, nonatomic) IBOutlet UILabel *timeL;
 @property (weak, nonatomic) IBOutlet UILabel *StudentL;
 @property (weak, nonatomic) IBOutlet UILabel *ClassL;*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyClassModel *mdoel = self.listArr[indexPath.row];

    MyClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyClassTableViewCell" forIndexPath:indexPath];
    [cell.titleImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,mdoel.catethumb]] placeholderImage:nil];
    cell.ClassName.text = mdoel.title;
    cell.timeL.text = [NSString stringWithFormat:@"%@创建",mdoel.addtime];
    cell.StudentL.text = [NSString stringWithFormat:@"累计学生%@人",mdoel.totalnum];
    cell.ClassL.text = [NSString stringWithFormat:@"累计课次%@课次",mdoel.teach_hours];
    cell.Delegate = self;
    cell.selectionStyle = 0;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

-(void)Selectindex:(NSInteger )index btn:(UIButton *)btn
{
    MyClassTableViewCell *cell = (MyClassTableViewCell *)btn.superview.superview;
    NSIndexPath *indexpath = [self.listV indexPathForCell:cell];

    if (index == 0)
    {
        [self StuList:indexpath];
    }else if (index == 1)
    {
        [self KeBiao:indexpath];
    }else if (index == 2)
    {
        [self KeLi:indexpath];
    }

}

-(void)StuList:(NSIndexPath *)indexpath
{
    MyClassModel *model = self.listArr[indexpath.row];
    ClassStuDentViewController *VC = [[ClassStuDentViewController alloc]init];
    VC.cid = model.cid;
    VC.Name = model.title;
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)KeBiao:(NSIndexPath *)indexpath
{
    MyClassModel *model = self.listArr[indexpath.row];
    KeBiaoListViewController *VC = [[KeBiaoListViewController alloc]init];
    VC.cid = model.cid;
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)KeLi:(NSIndexPath *)indexpath
{
    MyClassModel *model = self.listArr[indexpath.row];
    LessonTeacherViewController *vc = [[LessonTeacherViewController alloc]init];
    vc.cid = model.cid;
    [self.navigationController pushViewController:vc animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
